package clientView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import objectView.DepartmentCom;
import objectView.EmployeeDepCom;
import clientMain.Main;
import clientUtilGUI.KeyValue;
import clientUtilGUI.UtilityGUI;

public class Dep_View extends JScrollPane {

	private JList jListEm;
	private String majorRoomName;
	public static int depId;

	/**
	 * Create the panel.
	 * @param dep_Id
	 */
	public Dep_View(int dep_Id) {
		depId = dep_Id;
		DepartmentCom depInfo = Main.viewDep.getDep(dep_Id);
		
		Vector<EmployeeDepCom> listDataEm = Main.viewEm.getListEmInDep(dep_Id, true, true, 1, "Em_Id", true);
		
		for(int i = 0; i<listDataEm.size(); i++){
			if(listDataEm.get(i).getPosId() == 7 || listDataEm.get(i).getPosId() == 5){
				majorRoomName = listDataEm.get(i).getEmFullName();
				break;
			}
		}
		
		setBorder(new EmptyBorder(0, 0, 0, 0));
		setViewportView(initComponet(depInfo, listDataEm));
	}
	
	public JPanel initComponet(DepartmentCom depInfo, Vector<EmployeeDepCom> listEm){
		JPanel panel = new JPanel();
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
		panel.setLayout(gridBagLayout);
		
		JLabel lblHeader = new JLabel("Xưởng : "+depInfo.getDepName());
		lblHeader.setFont(UtilityGUI.fontHeader1);
		lblHeader.setForeground(UtilityGUI.colorHeader1);
		GridBagConstraints gbc_lblHeader = new GridBagConstraints();
		gbc_lblHeader.gridwidth = 3;
		gbc_lblHeader.insets = new Insets(10, 10, 15, 10);
		gbc_lblHeader.gridx = 0;
		gbc_lblHeader.gridy = 0;
		panel.add(lblHeader, gbc_lblHeader);
		
		JLabel lblDepName = new JLabel("Phòng Ban :");
		lblDepName.setFont(UtilityGUI.fontHeader2);
		GridBagConstraints gbc_lblDepName = new GridBagConstraints();
		gbc_lblDepName.anchor = GridBagConstraints.WEST;
		gbc_lblDepName.insets = new Insets(0, 25, 10, 5);
		gbc_lblDepName.gridx = 0;
		gbc_lblDepName.gridy = 1;
		panel.add(lblDepName, gbc_lblDepName);
		
		JLabel lblDepFullName = new JLabel(depInfo.getDepName());
		lblDepFullName.setFont(UtilityGUI.fontHeader2);
		lblDepFullName.setForeground(UtilityGUI.colorHeader2);
		GridBagConstraints gbc_lblDepFullName = new GridBagConstraints();
		gbc_lblDepFullName.anchor = GridBagConstraints.WEST;
		gbc_lblDepFullName.gridwidth = 2;
		gbc_lblDepFullName.insets = new Insets(0, 5, 10, 0);
		gbc_lblDepFullName.gridx = 1;
		gbc_lblDepFullName.gridy = 1;
		panel.add(lblDepFullName, gbc_lblDepFullName);
		
		JLabel lblDepDes = new JLabel("Giới thiệu :");
		lblDepDes.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblDepDes = new GridBagConstraints();
		gbc_lblDepDes.anchor = GridBagConstraints.WEST;
		gbc_lblDepDes.insets = new Insets(0, 25, 10, 5);
		gbc_lblDepDes.gridx = 0;
		gbc_lblDepDes.gridy = 2;
		panel.add(lblDepDes, gbc_lblDepDes);
		
		JLabel lblDepDesTxt = new JLabel(depInfo.getDepDescribe());
		lblDepDesTxt.setFont(UtilityGUI.fontLblForm);
		lblDepDesTxt.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblDepDesTxt = new GridBagConstraints();
		gbc_lblDepDesTxt.gridwidth = 2;
		gbc_lblDepDesTxt.anchor = GridBagConstraints.WEST;
		gbc_lblDepDesTxt.insets = new Insets(0, 5, 10, 0);
		gbc_lblDepDesTxt.gridx = 1;
		gbc_lblDepDesTxt.gridy = 2;
		panel.add(lblDepDesTxt, gbc_lblDepDesTxt);
		
		JLabel lblMajor = new JLabel("Trưởng phòng :");
		lblMajor.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblMajor = new GridBagConstraints();
		gbc_lblMajor.anchor = GridBagConstraints.WEST;
		gbc_lblMajor.insets = new Insets(0, 25, 10, 5);
		gbc_lblMajor.gridx = 0;
		gbc_lblMajor.gridy = 3;
		panel.add(lblMajor, gbc_lblMajor);
		
		JLabel lblMajorName = new JLabel(majorRoomName);
		lblMajorName.setFont(UtilityGUI.fontLblForm);
		lblMajorName.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblMajorName = new GridBagConstraints();
		gbc_lblMajorName.anchor = GridBagConstraints.WEST;
		gbc_lblMajorName.insets = new Insets(0, 5, 10, 5);
		gbc_lblMajorName.gridx = 1;
		gbc_lblMajorName.gridy = 3;
		panel.add(lblMajorName, gbc_lblMajorName);
		
		JLabel lblAddress = new JLabel("Địa Chỉ :");
		lblAddress.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblAddress = new GridBagConstraints();
		gbc_lblAddress.anchor = GridBagConstraints.WEST;
		gbc_lblAddress.insets = new Insets(0, 25, 10, 5);
		gbc_lblAddress.gridx = 0;
		gbc_lblAddress.gridy = 4;
		panel.add(lblAddress, gbc_lblAddress);
		
		JLabel lblDepAdd = new JLabel(depInfo.getDepAddress());
		lblDepAdd.setFont(UtilityGUI.fontLblForm);
		lblDepAdd.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblDepAdd = new GridBagConstraints();
		gbc_lblDepAdd.gridwidth = 2;
		gbc_lblDepAdd.anchor = GridBagConstraints.WEST;
		gbc_lblDepAdd.insets = new Insets(0, 5, 10, 0);
		gbc_lblDepAdd.gridx = 1;
		gbc_lblDepAdd.gridy = 4;
		panel.add(lblDepAdd, gbc_lblDepAdd);
		
		JLabel lblPhone = new JLabel("Điện Thoại :");
		lblPhone.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblPhone = new GridBagConstraints();
		gbc_lblPhone.anchor = GridBagConstraints.WEST;
		gbc_lblPhone.insets = new Insets(0, 25, 10, 5);
		gbc_lblPhone.gridx = 0;
		gbc_lblPhone.gridy = 5;
		panel.add(lblPhone, gbc_lblPhone);
		
		JLabel lblPhoneNum = new JLabel(depInfo.getDepPhone());
		lblPhoneNum.setFont(UtilityGUI.fontLblForm);
		lblPhoneNum.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblPhoneNum = new GridBagConstraints();
		gbc_lblPhoneNum.anchor = GridBagConstraints.WEST;
		gbc_lblPhoneNum.insets = new Insets(0, 5, 10, 5);
		gbc_lblPhoneNum.gridx = 1;
		gbc_lblPhoneNum.gridy = 5;
		panel.add(lblPhoneNum, gbc_lblPhoneNum);
		
		JLabel lblTotalEm = new JLabel("TS Nhân Viên :");
		lblTotalEm.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblTotalEm = new GridBagConstraints();
		gbc_lblTotalEm.anchor = GridBagConstraints.WEST;
		gbc_lblTotalEm.insets = new Insets(0, 25, 10, 5);
		gbc_lblTotalEm.gridx = 0;
		gbc_lblTotalEm.gridy = 6;
		panel.add(lblTotalEm, gbc_lblTotalEm);
		
		int totalEm = depInfo.getDepTotalEm();
		JLabel lblTotalNum = new JLabel(Integer.toString(totalEm));
		lblTotalNum.setFont(UtilityGUI.fontLblForm);
		lblTotalNum.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblTotalNum = new GridBagConstraints();
		gbc_lblTotalNum.anchor = GridBagConstraints.WEST;
		gbc_lblTotalNum.insets = new Insets(0, 5, 10, 5);
		gbc_lblTotalNum.gridx = 1;
		gbc_lblTotalNum.gridy = 6;
		panel.add(lblTotalNum, gbc_lblTotalNum);
		
		JLabel lblListEm = new JLabel("Danh Sách NV :");
		lblListEm.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblListEm = new GridBagConstraints();
		gbc_lblListEm.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblListEm.insets = new Insets(0, 25, 10, 5);
		gbc_lblListEm.gridx = 0;
		gbc_lblListEm.gridy = 7;
		panel.add(lblListEm, gbc_lblListEm);
		
		if(totalEm == 0){
			JLabel lblNoteListEm = new JLabel("Danh sách nhân viên hiện đang cập nhật...");
			lblNoteListEm.setFont(UtilityGUI.fontLblForm);
			lblNoteListEm.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lblNoteListEm = new GridBagConstraints();
			gbc_lblNoteListEm.anchor = GridBagConstraints.NORTHWEST;
			gbc_lblNoteListEm.insets = new Insets(0, 5, 10, 5);
			gbc_lblNoteListEm.gridx = 1;
			gbc_lblNoteListEm.gridy = 7;
			gbc_lblNoteListEm.gridwidth = 2;
			panel.add(lblNoteListEm, gbc_lblNoteListEm);
		}else{
			DefaultListModel model = new DefaultListModel();
			for(int i=0; i< listEm.size(); i++){
				String emId = listEm.get(i).getEmId();
				String emFullName = (i+1)+". "+listEm.get(i).getEmFullName();
				model.add(i, new KeyValue(emId, emFullName));
			}
			
			jListEm = new JList(model);
			UtilityGUI.styleJList(jListEm, totalEm);
			jListEm.setBackground(getBackground());
			jListEm.addMouseListener(new MouseListEm());
			GridBagConstraints gbc_jListEm = new GridBagConstraints();
			gbc_jListEm.anchor = GridBagConstraints.NORTHWEST;
			gbc_jListEm.insets = new Insets(0, 5, 10, 5);
			gbc_jListEm.gridx = 1;
			gbc_jListEm.gridy = 7;
			gbc_jListEm.gridwidth = 2;
			panel.add(jListEm, gbc_jListEm);
		}
		return panel;
	}
	 
	private class MouseListEm extends MouseAdapter{
		public void mouseClicked(MouseEvent mouseEvent) {
			 if(mouseEvent.isControlDown() && mouseEvent.getButton() == mouseEvent.BUTTON1){
				 String emId = ((KeyValue)jListEm.getSelectedValue()).getKey();
				 int index = Main.jTabbedPane.positionTab(UtilityGUI.tabEmInfo);
				 if(index == -1){
					 Main.jTabbedPane.addTab(new Employee_View(emId), UtilityGUI.tabEmInfo);
					 Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
				 }else{
					 Main.jTabbedPane.removeTabAt(index);
					 Main.jTabbedPane.addTab(new Employee_View(emId), UtilityGUI.tabEmInfo, index);
					 Main.jTabbedPane.setSelectedIndex(index);
				 }
			 }
		}
	} 

}
